package javaDemo15.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestUserMapper {
    public static void main(String[] args) throws IOException {
        //1.通过 Resources 类，加载 sqlMapConfig.xml，得到文件输入流对象
        InputStream inputStream = Resources.getResourceAsStream("C:\\Users\\zx\\IdeaProjects\\JAVAbase\\src\\javaDemo15\\mybatis\\sqlMapConfig.xml");
        //2.实例化会话工厂创建类 SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        //3.通过会话工厂创建类，读取上面的输入流，得到会话工厂 SqlSessionFactory 类
        SqlSessionFactory sessionFactory = factoryBuilder.build(inputStream);
        //4.使用 SqlSessionFactory 对象，创建 SqlSession 对象
        SqlSession session = sessionFactory.openSession();
        //5.获取接口的对象 UserMapper
        UserMapper userMapper = session.getMapper(UserMapper.class);
        //6.执行数据库的查询操作，输出用户信息
        System.out.println(userMapper);
        List<User> users = userMapper.findAllUsers();
        for (User user : users) {
            System.out.println(user);
        }
        //7.关闭会话，释放资源。
        session.close();
    }
}
